--[[
Project: HunterRP
File: npcs.lua
Author: Sebihunter
]]--

local tableNPCs = "npcs_"..game.GetMap()
gNPCNumber = 1

function initNPCs()
	local tExst = 1
	if (!sql.TableExists(tableNPCs)) then
		tExst = 0
		query = "CREATE TABLE "..tableNPCs.." ( unique_id int, npcid int, x float, y float, z float, rot float )"
		result = sql.Query(query)
		if (sql.TableExists(tableNPCs)) then
			Msg("Succes ! table 1 created \n")
		else
			Msg("Somthing went wrong with the "..tableNPCs.." query ! \n")
			Msg( sql.LastError( result ) .. "\n" )
		end
	end
	
	timer.Create("loadNPCs", 1, 1, function()		
		for i=1,100 do  
			local result = sql.Query("SELECT unique_id, npcid FROM "..tableNPCs.." WHERE unique_id = "..i)
			if (result) then
				local npcid = sql.QueryValue("SELECT npcid FROM "..tableNPCs.." WHERE unique_id = "..i)
				local x = sql.QueryValue("SELECT x FROM "..tableNPCs.." WHERE unique_id = "..i)
				local y = sql.QueryValue("SELECT y FROM "..tableNPCs.." WHERE unique_id = "..i)
				local z = sql.QueryValue("SELECT z FROM "..tableNPCs.." WHERE unique_id = "..i)
				local rot = sql.QueryValue("SELECT rot FROM "..tableNPCs.." WHERE unique_id = "..i)
				
				
				local npc = ents.Create("npc_eli")
				npc:SetPos(Vector(x,y,z))
				npc:SetAngles(Angle(0, rot, 0))
				if tonumber(npcid) == 1 then
					npc:SetModel( "models/barney.mdl" )
				elseif tonumber(npcid) == 2 then
					npc:SetModel( "models/mossman.mdl" )
				elseif tonumber(npcid) == 3 then
					npc:SetModel( "models/alyx.mdl" )					
				else
					npc:SetModel( "models/humans/group01/female_01.mdl" ) -- Standard model.
				end
				npc:Spawn()
			
				npc:SetHullType( HULL_HUMAN )
				npc:SetHullSizeNormal( )
				npc:SetNPCState( NPC_STATE_SCRIPT )
				npc:SetSolid(  SOLID_BBOX )
				npc:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD )
				npc:SetUseType( SIMPLE_USE )
				npc:DropToFloor()
				
				npc:SetNWInt("unique_id", gNPCNumber)
				npc:SetNWInt("npcid", tonumber(npcid))
				gNPCNumber = gNPCNumber + 1
			end
		end 
	
	end)
end

function onNPCUse(ply, npc)
	if npc:GetNWInt("npcid") == 1 then
		if ply:Team() == 1 then
			if getPlayerTeamInt(2) > getPlayerInt()*0.25 then
				showPlayerMessagebox( ply, "Police Recruitment", "There are enough officers out there at\nthe moment." )
			else
				showPlayerSelectionMessagebox( ply, "Police Recruitment", "Do you want to become a officer?", "Yes", "No", "setPlayerPolice" )
			end
		else
			if ply:Team() == 2 then
				showPlayerSelectionMessagebox( ply, "Police Recruitment", "Do you want to quit your job?", "Yes", "No", "setPlayerCitizen" )
			else
				showPlayerMessagebox( ply, "Police Recruitment", "Quit your job in order\nto become a officer." )
			end
		end
	elseif npc:GetNWInt("npcid") == 2 then
		if ply:Team() == 1 then
			if getPlayerTeamInt(3) > getPlayerInt()*0.25 then
				showPlayerMessagebox( ply, "Ambulance Recruitment", "There are enoughs medics out there\nat the moment." )
			else
				showPlayerSelectionMessagebox( ply, "Ambulance Recruitment", "Do you want to become a medic?", "Yes", "No", "setPlayerMedic" )
			end	
		else
			if ply:Team() == 3 then
				showPlayerSelectionMessagebox( ply, "Ambulance Recruitment", "Do you want to quit your job?", "Yes", "No", "setPlayerCitizen" )
			else
				showPlayerMessagebox( ply, "Ambulance Recruitment", "Quit your job in order\nto become a officer." )
			end
		end			
	elseif npc:GetNWInt("npcid") == 3 then
		if ply:Team() == 1 then
			showPlayerMessagebox( ply, "Mayor's Office", "You can't become the mayor\nat the moment." )
		else
			showPlayerMessagebox( ply, "Mayor's Office", "Quit your job in order\nto become the mayor." )
		end
	else
		showPlayerMessagebox( ply, "NPC Convo", "Good day sir!" )
	end
end

